package com.example.tuike.service;
import com.example.tuike.entity.Contents;
import com.example.tuike.mapper.ContentsMapper;
import jakarta.xml.bind.DatatypeConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/*** @Author supersaiya
 * @Description TODO

 * @Date 2025/2/2 21:18

 * @Version 1.0*/
@Service
public class ContentsService {

    @Autowired
    private ContentsMapper contentsMapper;

    public void createContents(Integer sourceId, List<Contents> contentsList) {
        // 遍历内容列表，逐个创建内容
        for (Contents contents : contentsList) {
            contents.setFeedId(sourceId); // 设置内容源ID
            System.out.println(contents.toString());
            //计算Hash值 根据连接 + 标题计算
            String Hash = calculateContentHash(contents.getTitle() + contents.getLink());
            contents.setHash(Hash);
            // 在插入前检查内容是否已存在
            if (contentsMapper.countByPermalinkAndHash(contents.getLink(), contents.getHash()) == 0) {
                // 调用持久化方法保存内容
                contentsMapper.insert(contents);
            } else {
                // 如果内容已存在，则根据需要执行其他操作，例如更新或忽略
                System.out.println("Content with permalink and hash already exists.");
            }
        }
    }

    public List<Contents> getAllContents() {
        return contentsMapper.findAll();
    }

    public Contents getContentsById(Long contentId) {
        return contentsMapper.findById(contentId);
    }
    public Contents createContent(Contents content) {
        contentsMapper.insert(content);
        return content;
    }
    //返回一个订阅源的内容记录
    public List<Contents> getContentsByUserIdAndFeedId(Integer feedId, Long cursor, Integer limit) {
        if (cursor == null) {
            // 如果 cursor 为 null，表示从最新的记录开始
            return contentsMapper.getContentsByFeedIdWithCursor(feedId, Long.MAX_VALUE, limit);
        } else {
            // 如果 cursor 不为 null，表示从 cursor 指定的 contentId 之后的记录开始
            return contentsMapper.getContentsByFeedIdWithCursor(feedId, cursor, limit);
        }
    }
    public List<Contents> getContentsByFeedId(Integer feedId) {
        return contentsMapper.findByFeedId(feedId);
    }
    public Contents updateContent(Contents content) {
        contentsMapper.update(content);
        return content;
    }

    public void deleteContent(Long contentId) {
        contentsMapper.delete(contentId);
    }
    private String calculateContentHash(String content) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(content.getBytes(StandardCharsets.UTF_8));
            return DatatypeConverter.printHexBinary(hash).toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }
}